<?php
require_once "header.php";

$city_id = isset($_POST['city_id']) ? intval($_POST['city_id']) : 0;
$mydirname = basename(dirname(__FILE__));

if (!$city[$city_id]) {
    redirect_header(XOOPS_URL . "/modules/$mydirname/" , 3, "_MD_JWEATHER_NONECITY");
    exit();
} 

if (empty($xoopsUser)) {
    if (setcookie("xoops_junkweather", $city_id, time() + 60 * 60 * 24 * 30, substr(XOOPS_URL,strpos(XOOPS_URL,$_SERVER['SERVER_NAME'])+strlen($_SERVER['SERVER_NAME']) )."/")) {
        redirect_header(XOOPS_URL . "/modules/$mydirname/" , 3, _MD_JWEATHER_INOK);
    } else {
        redirect_header(XOOPS_URL . "/modules/$mydirname/" , 3, _MD_JWEATHER_INNG);
    } 
    exit();
} else {
    $result = $xoopsDB->query("select * from " . $xoopsDB->prefix("junkweather") . ' WHERE user_id = ' . $xoopsUser->getVar('uid'));
    if ($xoopsDB->fetchRow($result)) {
        $update_cols = "city_id='" . $city_id . "'";
        $sql = 'update ' . $xoopsDB->prefix('junkweather') . ' set ' . $update_cols . ' where user_id = ' . $xoopsUser->getVar('uid');
    } else {
        $input_cols = "'" . $city_id . "', '" . $xoopsUser->getVar('uid') . "'";
        $db_cols = "city_id, user_id";
        $sql = "insert into " . $xoopsDB->prefix('junkweather') . " ( $db_cols ) values ( $input_cols )";
    } 
    if (!$result = $xoopsDB->query($sql)) {
        redirect_header(XOOPS_URL . "/modules/$mydirname/" , 3, _MD_JWEATHER_INNG);
        exit();
    } else {
        redirect_header(XOOPS_URL . "/modules/$mydirname/" , 3, _MD_JWEATHER_INOK);
        exit();
    } 
} 

?>